package com.weather.modules.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.weather.common.vo.WeatherResponse;
import com.weather.modules.service.CityDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service("getByIdCityDataServiceImpl")
public class CityDataServiceImpl implements CityDataService {

    private final static Logger logger = LoggerFactory.getLogger(CityDataServiceImpl.class);

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public WeatherResponse getCity(String cityId) {
        ObjectMapper mapper = new ObjectMapper();
//        String key = UrlConstants.WEATHER_URI + "citykey=" + cityId;

        String strBody = this.redisTemplate.opsForValue().get(cityId) +"";
        WeatherResponse weatherResponse = null;
        try {
            if (strBody != null && !strBody.isEmpty()) {
                weatherResponse = mapper.readValue(strBody, WeatherResponse.class);
            }
        } catch (IOException e) {
            //e.printStackTrace();
            logger.error("Error!",e);
        }
        return weatherResponse;
    }
}
